#!/usr/bin/python
# coding=utf-8
''' Copyright (c) [Year] [name of copyright holder]
   [Software Name] is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. '''
import os
import time
import random
import sys
import re


def click(x, y):
    cmd = "adb shell input tap {x1} {y1}".format(x1=x, y1=y)
    os.system(cmd)


def get_screen_size(*args):
    """获取当前屏幕的分辨率
       args 作为可填参数 获取传入设备的ID组
    """
    if not args:
        # 如果没有传入指定设备ID，直接获取当前默认的第一个设备屏幕分辨率信息
        size_file = os.popen("adb shell dumpsys window displays").read()
    else:
        # 获取匹配到指定设备ID获取对应的设备屏幕分辨率
        uid = args[0]
        size_file = os.popen(
            "adb -s {0} shell dumpsys window displays".format(uid)).read()
    if not size_file:
        # 获取当前设备分辨率失败
        print("获取不到设备分辨率,请检查 adb cmd")
        sys.exit()
    else:
        # 正则匹配当前设备分辨率
        sizeFind = re.search(r"(\d+)x(\d+)", size_file)
        if not sizeFind:
            print("正则获取不到设备分辨率")
            sys.exit()
        else:
            # 分割设备分辨率为数组
            get_screen = re.split(r"x", sizeFind.group())
            print(get_screen)
            # 转换数据类型
            size = [int(get_screen[0]), int(get_screen[1])]
            return size


def swipe_up(*args, t=500):
    """参考第三方的检查机制 默认使用 每次执行 2次在一定范围内的随机滚动手势"""
    '''通过自定义的修改 size对应的比例实现 上下左右 的对向滑动'''
    size = get_screen_size(*args)
    for idx in range(2):
        # print('循环')
        # print(idx)
        x1 = size[0] * (0.45+((idx+1)/100))
        y1 = size[1] * (0.60+((idx+1)/100))
        x2 = size[0] * (0.53+((idx+1)/100))
        y2 = size[1] * (0.30+((idx+1)/100))
        # print('输出滑动位置:')
        # print(x1, x2, y1, y2)
        # print(0.51+(idx+1)/100)
        if not args:
            time.sleep(0.5)
            os.system("adb shell input swipe {0} {1} {2} {3} {4}".format(
                x1, y1, x2, y2, t))
        else:
            time.sleep(0.5)
            uid = args[0]
            os.system(
                "adb -s %s shell input swipe {0} {1} {2} {3} {4}".format(uid, x1, y1, x2, y2, t))


def backapi():
    # 61 160
    # click(70, 171)
    # 此处是左上返回按钮
    # adb shell input keyevent
    cmd = "adb shell input keyevent 4"
    os.system(cmd)


def click_ok(x, y):
    click(x, y)
    # 此处是初始点击坐标
    time.sleep(1)
    # print("输出内容"+str(idx))
    # time.sleep(16)
    for idx in range(10):
        print('当前次数:'+str(idx))
        swipe_up()
        # swipe_up_vague()
    backapi()


def main():
    # click_ok()
    idx = 8
    # 初始化点击的次数
    for idx in range(idx):
        print("测试???:", str(idx))
        time.sleep(2)
        x = 900
        y = 1487
        click_ok(x, y)


if __name__ == "__main__":
    main()
    # size_file = os.popen("adb shell dumpsys window displays").read()
    # print('size_file')
    # print(size_file)
    # swipe_up()
    # backapi()
